package com.snsoft.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.snsoft.exception.DataIsNull;
import com.snsoft.exception.PromiseRefused;
import com.snsoft.exception.RepetOperate;
import com.snsoft.exception.UserExist;
import com.snsoft.service.manage.GetPrincipalsService;
import com.snsoft.service.manage.SetDownloadService;
import com.snsoft.util.Constants;
import com.snsoft.util.JsonUtils;

/**
 * 
 * @author 苏国启
 * 
 * @date 2017年11月18日 下午3:17:22
 * 
 * @Description TODO 仅超级管理员能调用的接口
 *
 */
@WebServlet("/manage")
public class ManageController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ManageController() {
		super();
	}

	/**
	 * GET请求接口分流
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		if ("getPrincipals".equals(method)) {
			this.getPrincipals(request, response);
		} else {
			response.getWriter().write(JsonUtils.objectToJson(null, Constants.CODE_ERROR, "请求异常！"));
		}
	}

	/**
	 * POST请求接口分流
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		if ("setDownload".equals(method)) {
			this.setDownload(request, response);
		} else {
			response.getWriter().write(JsonUtils.objectToJson(null, Constants.CODE_ERROR, "请求异常！"));
		}
	}

	/**
	 * 设置下载权限接口
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void setDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userID = request.getParameter("userID");
		String download = request.getParameter("downLoad");
		// 参数校验
		if (StringUtils.isBlank(userID) || !StringUtils.isNumeric(userID)) {
			response.getWriter().write(JsonUtils.objectToJson(null, Constants.CODE_ERROR, "选择的用户ID错误！"));
			return;
		}
		if (download == null || !download.matches("[0|1]{1}")) {
			response.getWriter().write(JsonUtils.objectToJson(null, Constants.CODE_ERROR, "设置的下载权限有错误！"));
			return;
		}
		try {
			new SetDownloadService().setDownload(userID, download);
			response.getWriter().write(JsonUtils.objectToJson(null, Constants.CODE_SUCCESS, Constants.JSON_SUCCESS));
			return;
		} catch (UserExist | PromiseRefused | RepetOperate e) {
			response.getWriter().write(JsonUtils.objectToJson(null, Constants.CODE_ERROR, "下载权限设置有错误！"));
			return;
		} catch (Exception e) {
			response.getWriter().write(JsonUtils.objectToJson(null, Constants.CODE_ERROR, "系统出现错误，请及时联系管理员解决问题！"));
			return;
		}
	}

	/**
	 * 获取负责人列表接口
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getPrincipals(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			List<Map<String, Object>> data = new GetPrincipalsService().getPrincipals();
			response.getWriter().write(JsonUtils.objectToJson(data, Constants.CODE_SUCCESS, Constants.JSON_SUCCESS));
			return;
		} catch (DataIsNull e) {
			response.getWriter().write(JsonUtils.objectToJson(null, Constants.CODE_ERROR, e.getMessage()));
			return;
		} catch (Exception e) {
			response.getWriter().write(JsonUtils.objectToJson(null, Constants.CODE_ERROR, "系统出现错误，请及时联系管理员解决问题！"));
			return;
		}
	}
}
